{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "X = 2 * np.random.rand(100,1)\n",
    "y = 4 + 3 * X + np.random.randn(100,1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "X_b = np.c_[np.ones((100,1)), X]    # add x0 = 1 to each instance\n",
    "theta_best = np.linalg.inv(X_b.T.dot(X_b)).dot(X_b.T).dot(y)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 3.88117227],\n",
       "       [ 2.96826321]])"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "theta_best"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "X_new = np.array([[0], [2]])\n",
    "X_new_b = np.c_[np.ones((2,1)), X_new]  # add x0 = 1 to each instance\n",
    "y_predict = X_new_b.dot(theta_best)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 3.88117227],\n",
       "       [ 9.8176987 ]])"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "y_predict"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "from sklearn.linear_model import LinearRegression"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "LinearRegression(copy_X=True, fit_intercept=True, n_jobs=1, normalize=False)"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "lin_reg = LinearRegression()\n",
    "lin_reg.fit(X, y)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(array([ 3.88117227]), array([[ 2.96826321]]))"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "lin_reg.intercept_, lin_reg.coef_"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 3.88117227],\n",
       "       [ 9.8176987 ]])"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "lin_reg.predict(X_new)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import matplotlib.pyplot as plt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[0, 2, 0, 16]"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "plt.plot(X, y, \"b.\")\n",
    "plt.plot(X_new, y_predict, \"r-\")\n",
    "plt.axis([0, 2, 0, 16])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAXwAAAD8CAYAAAB0IB+mAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAHqdJREFUeJzt3X2UXHWd5/H3N510SICAJOGZJoA8hYgMaWI6STcN0dmA\nCvuAO3DEDJG1D8KiKOAKLjhnGCaOurOwy3qcHCfjsDLMOILCOCIga1sN6WRIMEAwoJEnw4RJeH4w\nD3T6u3/c6nR1p6v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4kWfpgpFs4HbvfAauBAMzss5LZJqfha7r7K3V8v/ria4LMFtRbn\nPcnU+znKRcCdKdVSlrsXgNfGeUgW9s2KdWZk3wzzfpaTqfdzlHrtm1vc/bHiv98GNhLMYFAq0f0z\nicAfa4qF0UWXe0yYbZNS7WtdSnBkHeLAz8xsnQXTRaQlbJ0Liqd495nZKVVum4TQr2VmU4ElwF0l\nN9fq/awkC/tmteq1b4ZV730ztKzsm2Y2C/gDYM2ouxLdP1OfWqERmdlZBH9Ui0puXuTuL5nZwcCD\nZvZ0sRVRD48Bbe7+jpmdC/wIOL5OtYTxceARdy9tcWXp/WwY2jcTV/d908z2IzjgXOXub6X1OpBM\nCz/MFAvlHlPL6RlCvZaZnQp8Bzjf3V8dut3dXyp+3wr8kOCUqi51uvtb7v5O8d8/ASaZ2Yww29ay\nzhIXMuqUuYbvZyVZ2DdDycC+WVFG9s1q1HXfNLNJBGF/h7vfPcZDkt0/E7jwMBF4FjiG4YsHp4x6\nzEcZeeHhX8Jum+AFkjB1tgGbgAWjbt8X2L/k36uAJXWs81CGPzQ3D3ix+N5m6v0sPu4Agr7Ufevx\nfhZfYxblLzLWfd8MWWfd982QddZ93wxTZxb2zeL7cjtwyziPSXT/jN2l42WmWDCzy4r3fxv4CcHV\n5k3A74Fl420bt6YYdd4ITAe+ZWYAAx7MpHcI8MPibROBv3P3n9axzguAz5rZALAduNCDvSBr7yfA\nfwAecPd3Szav2ftpZncSjByZYWabga8Ck0pqrPu+GbLOuu+bIeus+74Zsk6o874JLAQ+BTxpZuuL\nt11PcHBPZf/U1AoiIjmhT9qKiOSEAl9EJCcU+CIiOaHAFxHJCQW+iEhOKPBFRHJCgS8ikhP/H53H\ndxvCy+BiAAAAAElFTkSuQmCC\n",
      "text/plain": [
       "<matplotlib.figure.Figure at 0xf9bb6a198>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "eta = 0.1\n",
    "n_iterations = 1000\n",
    "m = 100"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "theta = np.random.randn(2,1)  # random initialization"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "for iteration in range(n_iterations):\n",
    "    gradients = 2/m * X_b.T.dot(X_b.dot(theta) - y)\n",
    "    theta = theta - eta * gradients"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 4.02220286]\n",
      " [ 2.9470356 ]]\n"
     ]
    }
   ],
   "source": [
    "print(theta)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.3"
  },
  "toc": {
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
